import { wxappconstant } from '../common/wxconstant'
import { ajaxGet, ajaxPost } from 'wxAjaxUtil';
import { Toast } from 'antd-mobile'

export default store => next => action => {
    const getd = action['wxget'];
    const postd = action['wxpost'];
    const pathKey = action['pathKey'];

    console.log("已捕捉到action，在这里处理ajax请求", getd, wxappconstant)
    if (getd) {
        ajaxGet(action.url).then(res => {
            if (res.code !== "0") {
                switch (getd.type) {
                    //如果获取失败，只需要在这加case判断，然后添加提示即可
                    case "WXGETSUBPRODUCT":
                        Toast.fail('获取子产品失败', 1);
                        break;
                }
                return next({ type: getd.type });  //失败不返回data
            } else {
                return next({ type: getd.type, data: res })  //成功返回data
            }
        })
    } else if (postd) {
        if (pathKey) {  //吾享接口
            ajaxPost(action.url, action.data, action.pathKey).then(res => {
                if (res.errorCode != 200) {
                    switch (postd.type) {
                        //如果获取失败，只需要在这加case判断，然后添加提示即可
                        case "WXZHCTGETLIST":
                            Toast.fail('获取已开通列表失败', 1);
                            break;
                    }
                    return next({ type: postd.type });  //失败不返回data
                } else {
                    return next({ type: postd.type, data: res })  //成功返回data
                }
            })
        } else {   //商龙云接口

        }
    } else {
        return next(action)
    }
}